<template>
  <div class="goods-image">
  <!-- 大图 -->
  <div v-show='!isOutside' class="large" >
    <img :src="images[activeIndex]" alt="" :style="{left:-2*imgX+'px',top:-2*imgY+'px'}"/>
  </div>
    <div class="middle" ref='target' @mousemove='move'>
    <!-- 商品展示静态图 -->
      <img :src="images[activeIndex]" alt="" />
      <!-- 遮罩层 -->
      <div v-show='!isOutside' class="layer" :style="{left:imgX+'px',top:imgY+'px'}" ></div>
    </div>
    <ul class="small">
      <li v-for="(item,index)  in images" :key="index" @mouseenter="activeIndex=index">
      <!-- 侧边小图 -->
        <img :src="item" alt="" />
      </li>
    </ul>
  </div>
</template>

<script>
import { useMouseInElement } from '@vueuse/core'
import { ref } from 'vue'
export default {
  name: 'GoodsImage',
  props: {
    images: {
      type: Array,
      default: () => []
    }
  },
  setup () {
    const activeIndex = ref(0)
    const target = ref(null)
    const imgX = ref(0)
    const imgY = ref(0)
    const { elementX, elementY, isOutside } = useMouseInElement(target)
    const move = () => {
      imgX.value = elementX.value - 100
      imgY.value = elementY.value - 100
      if (imgX.value < 0) imgX.value = 0
      if (imgY.value < 0) imgY.value = 0
      if (imgX.value > 200) imgX.value = 200
      if (imgY.value > 200) imgY.value = 200
    }
    return { activeIndex, target, imgX, imgY, isOutside, move }
  }
}
</script>

<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  z-index: 500;
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    box-shadow: 0 0 10px rgba(0,0,0,0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    overflow: hidden;
    img{
      background-color: #fff;
      position: absolute;
      max-width: 800px;
      max-height: 800px;
    }
  }
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    position: relative;
    cursor: move;
    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0,0,0,.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
